package main.network;

import android.content.Context;
import android.telephony.TelephonyManager;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户登录模块
 * Created by 凯 on 2015/10/16.
 */
public class Login {
    // 一般是界面，获取到结果
    private Delegate delegate;
    // 用户名
    private String userName;
    // 密码
    private String password;
    // 设备ID
    private String UUID;

    // 结果
    private ResponseSignIn responseSignIn;

    public ResponseSignIn getResponseSignIn() {
        return responseSignIn;
    }

    public void setResponseSignIn(ResponseSignIn responseSignIn) {
        this.responseSignIn = responseSignIn;
    }

    public Delegate getDelegate() {
        return delegate;
    }

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Login(Delegate delegate, String userName, String password) {
        this.delegate = delegate;
        this.userName = userName;
        this.password = password;
        // 发送用户信息
        String imei = ((TelephonyManager) MyApplication.share().getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
        imei = imei + MyApplication.share().getPackageName();
        this.UUID = java.util.UUID.nameUUIDFromBytes(imei.getBytes()).toString().replaceAll("-", "").toString();
    }

    // 登录请求
    public void login() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/User/UserSignIn";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (!response.startsWith("{")) return;
                        Gson gson = new Gson();
                        responseSignIn = gson.fromJson(response, ResponseSignIn.class);
                        if (delegate != null)
                            delegate.onResponse(response, Login.this);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                if (delegate != null)
                    delegate.onError(error, Login.this);
            }

        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("userName", userName);
                map.put("password", password);
                map.put("UUID", UUID);
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    /**
     * 由http请求获取String 的客户端实现
     * Created by 凯 on 2015/10/16.
     */
    public static interface Delegate {
        public void onResponse(String s, Login login);

        public void onError(VolleyError volleyError, Login login);
    }

    public static class OwnUser {
        private String user_ID;
        private String user_NickName;
        private String user_RealName;
        private String user_Role;
        private boolean user_Gender;

        public String getUser_ID() {
            return user_ID;
        }

        public void setUser_ID(String user_ID) {
            this.user_ID = user_ID;
        }

        public String getUser_NickName() {
            return user_NickName;
        }

        public void setUser_NickName(String user_NickName) {
            this.user_NickName = user_NickName;
        }

        public String getUser_RealName() {
            return user_RealName;
        }

        public void setUser_RealName(String user_RealName) {
            this.user_RealName = user_RealName;
        }

        public String getUser_Role() {
            return user_Role;
        }

        public void setUser_Role(String user_Role) {
            this.user_Role = user_Role;
        }

        public boolean isUser_Gender() {
            return user_Gender;
        }

        public void setUser_Gender(boolean user_Gender) {
            this.user_Gender = user_Gender;
        }


    }

    public static class WorkLocInfo {
        private String longitude;        // 经度
        private String latitude;        // 纬度
        private String workLoc_Name;

        public String getLongitude() {
            return longitude;
        }

        public void setLongitude(String longitude) {
            this.longitude = longitude;
        }

        public String getLatitude() {
            return latitude;
        }

        public void setLatitude(String latitude) {
            this.latitude = latitude;
        }

        public String getWorkLoc_Name() {
            return workLoc_Name;
        }

        public void setWorkLoc_Name(String workLoc_Name) {
            this.workLoc_Name = workLoc_Name;
        }
    }

    public static class ResponseSignIn {
        private String status;
        private OwnUser responseUser;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public OwnUser getResponseUser() {
            return responseUser;
        }

        public void setResponseUser(OwnUser responseUser) {
            this.responseUser = responseUser;
        }
    }

    public static class SignInStatus {
        public static final String FAILED = "0";        // 登录错误
        public static final String SUCCESS = "1";        // 成功
        public static final String LOGINED = "2";        // 用户已登录
        public static final String NOTEXIST = "3";        // 用户不存在
        public static final String PWERROR = "4";        // 密码错误
    }

}


